<template>
  <el-button @click="handleAdd" v-if="$filters.auth($filters.PrivilegeType.Add)" class="button-header"  type="primary" :icon="Plus">添加管理员</el-button>
  <el-form inline :model="query" class="filter" label-width="100px">
    <el-form-item label="角色名称：">
      <el-col :span="22">
        <el-input v-model="query.userName" placeholder="请输入角色名称" />
      </el-col>
    </el-form-item>
    <el-form-item label="所属角色：">
      <el-col :span="22">
        <el-select v-model="query.roleId" placeholder="请选择" clearable>
          <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-col>
    </el-form-item>
    <el-form-item class="filter-actions">
          <el-button class="button-small" type="primary" @click="handleSearch">筛选</el-button>
          <el-button type="text" class="button-reset" @click="handleClear">重置筛选条件</el-button>
    </el-form-item>
  </el-form>
  <grid url="manager/list" :params="query" ref="datalist">
    <el-table-column prop="userName" label="管理员账号" />
    <el-table-column prop="name" label="管理员角色" />
    <el-table-column prop="createDate" label="创建日期" />
    <el-table-column label="操作" align="right" width="200"  v-if="$filters.auth($filters.PrivilegeType.Edit) || $filters.auth($filters.PrivilegeType.Delete)">
      <template #default="scope">
        <el-button type="text" v-if="$filters.auth($filters.PrivilegeType.Edit)" @click="handleEdit(scope.row)">编辑</el-button>
        <el-popconfirm :width="300" v-if="$filters.auth($filters.PrivilegeType.Delete) && scope.row.id > 1 " class="btn-popconfirm" @confirm="handleDelete(scope.row.id)" confirmButtonText="确认" cancelButtonText="取消" title="确定要删除该品牌吗？">
          <template #reference>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </grid>
  <el-dialog append-to-body v-model="dialogVisible" :title="form.id > 0 ? '修改管理员':'添加管理员'" width="30%">
    <el-form ref="postForm" label-width="150px" class="ruleForm" :model="form" :rules="rules">
        <el-form-item label="管理员账号：" prop="username">
          <el-col :span="22">
            <el-input v-model="form.userName" maxlength="20" :disabled="form.id > 0" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
        </el-form-item>
        <el-form-item label="密码：" :key="2" :prop="form.id > 0 ? 'empty' : 'password'">
          <el-col :span="22">
            <el-input v-model="form.password" type="password" autocomplete="new-password" maxlength="20" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
        </el-form-item>

        <el-form-item label="所属角色：" prop="roleId">
          <el-col :span="22">
            <el-select v-model="form.roleId" placeholder="请选择">
              <el-option key="0" label="超级管理员" :value="0" />
              <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave(postForm)">确定</el-button>
        </span>
      </template>
  </el-dialog>

</template>
<script lang="ts" setup>
import Grid from '@/components/Grid.vue'
import {Plus} from '@element-plus/icons-vue'
import { RoleList } from '@/api/role' 
import {ManagerSave, ManagerDelete} from '@/api/manager'
import {ref,reactive,onMounted} from 'vue'
import {ElForm, ElMessage} from 'element-plus'
const datalist = ref<InstanceType<typeof Grid>>()
const query =ref<ManagerQuery>({
  userName:'',
  roleId: undefined
})
const roles = ref<Role[]>([])
const dialogVisible = ref<boolean>(false)
const rules = reactive({
    userName: [{ required: true, message: '请输入员工账号', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    roleId: [{ required: true, message: '请输入所属角色', trigger: 'blur' }]
})
const form = ref<ManagerEditor>({
  id: 0,
  userName:'',
  roleId:0,
  password:''
})
type FormInstance = InstanceType<typeof ElForm>
const postForm = ref<FormInstance>()
const handleAdd =():void => {
  dialogVisible.value = true
  form.value = {
    id: 0,
    userName:'',
    roleId:0,
    password:''
  }
}
const handleEdit = (manager: ManagerEditor):void => {
  form.value = {
    id:manager.id,
    userName:manager.userName,
    roleId:manager.roleId,
    password:''
  }
  dialogVisible.value = true
}
/**
 * 获取角色列表
 */
const getRoleList =():void => {
  RoleList().then((res:ServiceResponse<Role[]>) => {
    roles.value = res.data ?? []
  })
}
const handleSearch =():void => {
  datalist.value?.loadData()
}
const handleClear =():void => {
  query.value .userName =''
  query.value .roleId = undefined
  query.value = {
    userName:'',
    roleId: undefined
  }
  datalist.value?.loadData()
}
/**
 * 保存
 */
 const handleSave  = (formEl: FormInstance | undefined): void => {
  if (!formEl) return
  formEl.validate((valid) => {
    if(valid) {
      ManagerSave(form.value).then((res:ServiceResponse<any>) => {
        if(res.success) {
          ElMessage.success('保存成功')
          datalist.value?.loadData()
          dialogVisible.value = false
        } else  ElMessage.error(res.msg)
      })
    }
  })

}
/**
 * 删除
 */
const handleDelete = (id:number):void => {
  ManagerDelete(id).then(((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('删除成功')
          datalist.value?.loadData()
          dialogVisible.value = false
        } else  ElMessage.error(res.msg)
  }))
}
onMounted(() => {
  getRoleList()
})
</script>
<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
@import "@/assets/styles/tablePage.scss";
</style>